<template>
	<view style="padding: 10px;">
		<uni-forms :modelValue="formData">
			<uni-forms-item label="序号" name="serial_no" required label-align="right">
				<uni-easyinput type="text" v-model="formData.serial_no" placeholder="请输入序号" />
			</uni-forms-item>
			<uni-forms-item label="姓名" name="name" required label-align="right">
				<uni-easyinput type="text" v-model="formData.name" placeholder="请输入姓名" />
			</uni-forms-item>
			<uni-forms-item label="金额" name="money" required label-align="right">
				<uni-easyinput type="number" v-model="formData.money" placeholder="请输入金额" />
			</uni-forms-item>
			<uni-forms-item label="地址" name="address" required label-align="right">
				<uni-easyinput type="text" v-model="formData.address" placeholder="请输入地址" />
			</uni-forms-item>
			<uni-forms-item label="日期" name="date" required label-align="right">
				<uni-datetime-picker type="date" :clear-icon="false" v-model="formData.date" />
			</uni-forms-item>
			<uni-forms-item label="账本" name="source" required label-align="right">
				<uni-easyinput type="text" v-model="formData.source" placeholder="请输入账本" />
			</uni-forms-item>
			<uni-forms-item label="说明" name="remark" label-align="right">
				<uni-easyinput type="textarea" v-model="formData.remark" placeholder="请输入说明" />
			</uni-forms-item>
		</uni-forms>
		<button type="primary" @click="submitForm">保存</button>
	</view>
</template>
<script>
	import dbOper from '@/module/accountSqlOper'
	import modal from "@/utils/modal.js"
	export default {
		data() {
			return {
				formData: {
					id: null,
					serial_no: null,
					name: null,
					money: null,
					address: null,
					date: null,
					source: null,
					remark: null
				}
			}
		},
		onLoad(option) {
			if (option.id != -1) {
				this.formData.id = option.id
				this.getDate()
			}
		},
		methods: {
			getDate() {
				dbOper.selectById(this.formData.id)
					.then(res => {
						this.formData = res
					})
			},
			submitForm() {
				if (!this.formData.serial_no || !this.formData.name || !this.formData.money || !this.formData.address || !
					this.formData.source) {
					modal.msgError("请检查必填项")
					return
				}
				modal.confirm("是否要保存该数据")
					.then(res => {
						modal.loading()
						let fun = this.formData.id ? dbOper.updateRow(this.formData) : dbOper.insertRow(this.formData)
						fun.then(res => {
								modal.closeLoading()
								uni.navigateBack()
							})
							.catch(err => {
								modal.closeLoading()
							})
					})
			}
		}
	}
</script>